﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using HumanManagement.DataAccess;
using HumanManagement.BusinessObjects;

namespace BSC_HumanManagement.Pages
{
    public partial class MenuLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ltlMenuleft.Text = Load_MenuHtml(Load_MenuData());
            LoadThongTinHienThi();
        }

        private void LoadThongTinHienThi()
        {
            try
            {
                PersonalProfile _objPersonalProfile = (PersonalProfile)Session["PersonalInfomation"];
                this.lblXinChaoHoTen.Text = _objPersonalProfile.FullName;
                string base64String = Convert.ToBase64String(_objPersonalProfile.Picture, 0, _objPersonalProfile.Picture.Length);
                this.imgAnhDaiDienLeft.ImageUrl = "data:image/jpeg;base64," + base64String;
            }
            catch (Exception ex) { }
        }

        // lấy danh sách các menu chức năng theo v_UserFunction của người sử dụng
        private IDataReader Load_MenuData()
        {
            string _session_username = Convert.ToString(Session["login_username"]);
            return SqlHelper.ExecuteReader(Code.Staticvariable.connectString, CommandType.Text, "Select FunctionCategory, FunctionName, SubFunctionName " +
                "From v_UserFunction Where UserName = '"+ Convert.ToString( Session["login_username"])+"' order by order1, order2, order3");
        }

        // trả về HTML menu left
        private string  Load_MenuHtml(IDataReader _reader)
        {
            string result = "";
            string _FunctionCategory = "";
            string _FunctionName = "";
            bool firstHtml = false;
          
            while (_reader.Read())
            {
                // Nếu đây là lần chạy đầu, khởi tạo cây thư mục menu
                if (Convert.ToString(_reader["FunctionCategory"]) != "" && firstHtml == false)
                {
                    _FunctionCategory = Convert.ToString(_reader["FunctionCategory"]);
                    _FunctionName = Convert.ToString(_reader["FunctionName"]);
                    result += "<li class='nv active'><a><i class='fa fa-home'></i>" + _FunctionCategory + "<span class='fa fa-chevron-down'></span></a>" +
                              "<ul class='nav child_menu' style='display: none'>" +
                              "<li>" +
                                  "<a href='/Default.aspx?FunctionName=" + Convert.ToString(_reader["FunctionName"]) + "'>" + Convert.ToString(_reader["FunctionName"]) + "</a>" +
                              "</li>";
                    firstHtml = true;
                }

                // kiểm tra nếu trùng FunctionCategory thì check FunctionName để tạo FunctionName mới , nếu ko trùng thì tạo FunctionCategory mới và đóng FunctionCategory cũ
                if (_FunctionCategory != Convert.ToString(_reader["FunctionCategory"]) && firstHtml==true)
                {
                    result += "</ul></li>";
                    result += "<li class='nv active'><a><i class='fa fa-home'></i>" + Convert.ToString(_reader["FunctionCategory"]) + "<span class='fa fa-chevron-down'></span></a>" +
                               "<ul class='nav child_menu' style='display: none'>" +
                              "<li>" +
                                  "<a href='/Default.aspx?FunctionName=" + Convert.ToString(_reader["FunctionName"]) + "'>" + Convert.ToString(_reader["FunctionName"]) + "</a>" +
                              "</li>";
                    firstHtml = false;
                }
                else
                {
                    if (_FunctionName != Convert.ToString(_reader["FunctionName"]))
                    {
                        result += "<li>" +
                                    "<a href='/Default.aspx?FunctionName=" + Convert.ToString(_reader["FunctionName"]) + "'>" + Convert.ToString(_reader["FunctionName"]) + "</a>" +
                                  "</li>";
                    }
                }
                _FunctionName = Convert.ToString(_reader["FunctionName"]);
            }
            return result;
        }
    }
}